package shuimin.weixin.message;

import pond.web.EndToEndException;
import shuimin.weixin.Config;
import shuimin.weixin.Constant;
import shuimin.weixin.HTTP;
import shuimin.weixin.Weixin;
import shuimin.weixin.message.model.menu.Menu;
import shuimin.weixin.security.AccessToken;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by sky on 16/5/18.
 */
public class WeixinMenu extends Weixin{

    public void createMenu(Menu menu,String app) throws UnsupportedEncodingException {
        String url=Constant.QY_API_URL_PREFIX+Constant.MENU_CREATE_URL;
        url+="access_token="+ AccessToken.getAccessToken()+"&agentid="+ Config.getAgentId(app);
        HTTP.postJson(url,menu.parseToWeixin(),resp->{
            Map<String,Object> map=HTTP.parseRespToJson(resp);
            Object code=map.get("errcode");
            if(!code.equals(0)){
                throw new EndToEndException(400, (String) map.get("errmsg"));
            }
        });
    }
}
